package com.sskj.wallet.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.WalletDetailBean;
import com.sskj.wallet.presenter.TransferPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;

/**
 * 总账户可以划转至4个子账户
 * <p>
 * 子账户只能划转至总账户
 *
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.WALLET_ACTIVITY_TRANSFER)
public class TransferActivity extends BaseActivity<TransferPresenter> {


    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.iv_right)
    ImageView ivRight;
    @BindView(R2.id.tv_choose_coin)
    TextView tvChooseCoin;
    @BindView(R2.id.iv_exchange)
    ImageView ivExchange;
    @BindView(R2.id.tv_top)
    TextView tvTop;
    @BindView(R2.id.iv_top)
    ImageView ivTop;
    @BindView(R2.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R2.id.tv_bottom)
    TextView tvBottom;
    @BindView(R2.id.iv_bottom)
    ImageView ivBottom;
    @BindView(R2.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R2.id.et_amount)
    EditText etAmount;
    @BindView(R2.id.tv_usable)
    TextView tvUsable;
    @BindView(R2.id.btn_transfer)
    Button btnTransfer;
    @Autowired
    String walletType;

    private int transferType = 0;

    private String transferWallet;

    private List<WalletDetailBean.ListBean> walletCoins = new ArrayList<>();

    List<String> coins = new ArrayList<>();
    List<String> wallets = new ArrayList<>();
    private Map<String, String> walletIds = new HashMap<>();

    private String transferFrom;//发起划转钱包
    private String transferTo;//接收划转钱包

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_transfer;
    }

    @Override
    public TransferPresenter getPresenter() {
        return new TransferPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        ivRight.setImageResource(R.mipmap.icon_jilu);
        ivRight.setVisibility(View.VISIBLE);
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_transferActivity1));
        etAmount.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(5)});
        if (walletType.equals(Constans.WALLET_ALL)) {
            transferWallet = App.INSTANCE.getString(R.string.wallet_transferActivity2);
            ivBottom.setVisibility(View.VISIBLE);
        } else if (walletType.equals(Constans.WALLET_HUOQI)) {
            transferWallet = App.INSTANCE.getString(R.string.wallet_transferActivity3);
        } else if (walletType.equals(Constans.WALLET_JIEDAI)) {
            transferWallet = App.INSTANCE.getString(R.string.wallet_transferActivity4);
        } else if (walletType.equals(Constans.WALLET_LIANGHUA)) {
            transferWallet = App.INSTANCE.getString(R.string.wallet_transferActivity5);
        } else if (walletType.equals(Constans.WALLET_DAPP)) {
            transferWallet = App.INSTANCE.getString(R.string.wallet_transferActivity6);
        }
        wallets.add(App.INSTANCE.getString(R.string.wallet_transferActivity2));
        wallets.add(App.INSTANCE.getString(R.string.wallet_transferActivity3));
        wallets.add(App.INSTANCE.getString(R.string.wallet_transferActivity4));
        wallets.add(App.INSTANCE.getString(R.string.wallet_transferActivity5));
        wallets.add(App.INSTANCE.getString(R.string.wallet_transferActivity6));
        walletIds.put(App.INSTANCE.getString(R.string.wallet_transferActivity2), Constans.WALLET_ALL);
        walletIds.put(App.INSTANCE.getString(R.string.wallet_transferActivity3), Constans.WALLET_HUOQI);
        walletIds.put(App.INSTANCE.getString(R.string.wallet_transferActivity4), Constans.WALLET_JIEDAI);
        walletIds.put(App.INSTANCE.getString(R.string.wallet_transferActivity5), Constans.WALLET_LIANGHUA);
        walletIds.put(App.INSTANCE.getString(R.string.wallet_transferActivity6), Constans.WALLET_DAPP);
        wallets.remove(transferWallet);
        tvTop.setText(transferWallet);
        tvBottom.setText(wallets.get(0));
        transferFrom = walletType;
        transferTo = walletIds.get(wallets.get(0));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mPresenter != null) {

            getWalletData(walletType);
        }
    }

    @Override
    public void initData() {
        coins.add(Constans.COIN_BTC);
        coins.add(Constans.COIN_ETH);
        coins.add(Constans.COIN_LTC);
        coins.add(Constans.COIN_USDT);
        coins.add(Constans.COIN_AICASH);

        getWalletData(walletType);
        ClickUtil.click(300, tvChooseCoin, () -> {
            if (walletCoins.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_transferActivity7));
                return;
            }
            BottomDialog.chooseCoins(TransferActivity.this, coins, tv -> {
                tvChooseCoin.setText(tv);
                Observable.fromIterable(walletCoins)
                        .filter(listBean -> listBean.getP_name().equals(tv)).compose(RxSchedulersHelper.io_main())
                        .subscribe(listBean -> tvUsable.setText(App.INSTANCE.getString(R.string.wallet_transferActivity8) + CommonUtil.dealCoinNum(listBean.getUsable(), tv) + tv));
            });
        });
        ClickUtil.click(300, ivExchange, () -> {
            if (transferType == 0) {
                if (!walletType.equals(Constans.WALLET_ALL)) {
                    ivTop.setVisibility(View.INVISIBLE);
                    ivBottom.setVisibility(View.INVISIBLE);
                } else {
                    ivTop.setVisibility(View.VISIBLE);
                    ivBottom.setVisibility(View.INVISIBLE);
                }
                tvTop.setText(wallets.get(0));
                tvBottom.setText(transferWallet);
                transferType = 1;

                transferFrom = walletIds.get(wallets.get(0));
                transferTo = walletType;

            } else {
                if (!walletType.equals(Constans.WALLET_ALL)) {
                    ivTop.setVisibility(View.INVISIBLE);
                    ivBottom.setVisibility(View.INVISIBLE);
                } else {
                    ivTop.setVisibility(View.INVISIBLE);
                    ivBottom.setVisibility(View.VISIBLE);
                }
                tvTop.setText(transferWallet);
                tvBottom.setText(wallets.get(0));
                transferType = 0;

                transferFrom = walletType;
                transferTo = walletIds.get(wallets.get(0));
            }
            getWalletData(transferFrom);
        });
        ClickUtil.click(300, rlTop, () -> {
            if (!walletType.equals(Constans.WALLET_ALL)) {
                return;
            }
            if (transferType == 1) {
                BottomDialog.chooseCoins(TransferActivity.this, wallets, tv -> {
                    tvTop.setText(tv);
                    transferFrom = walletIds.get(tv);
                    getWalletData(transferFrom);
                });
            }
        });
        ClickUtil.click(300, rlBottom, () -> {
            if (!walletType.equals(Constans.WALLET_ALL)) {
                return;
            }
            if (transferType == 0) {
                BottomDialog.chooseCoins(TransferActivity.this, wallets, tv -> {
                    tvBottom.setText(tv);
                    transferTo = walletIds.get(tv);
                });
            }
        });
        //划转记录
        ClickUtil.click(300, ivRight, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_TRANSFER)
                    .withString(Constans.WALLET_TYPE, walletType).navigation();
        });
        /*划转*/
        ClickUtil.click(300, btnTransfer, () -> {
            if (isEmpty(transferFrom) || isEmpty(transferTo)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_transferActivity9));
                return;
            }
            if (isEmpty(tvChooseCoin)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_transferActivity10));
                return;
            }
            if (isEmpty(etAmount)) {
                toastHint(etAmount);
                return;
            }
            mPresenter.toTransfer(getText(tvChooseCoin).toLowerCase(), getText(etAmount), transferTo, transferFrom);

        });
    }

    private void getWalletData(String walletId) {

        if (walletId.equals(Constans.WALLET_HUOQI)) {
            mPresenter.getWalletDetail(2);
        } else if (walletId.equals(Constans.WALLET_JIEDAI)) {
            mPresenter.getWalletDetail(3);
        } else {
            mPresenter.getWalletDetail(Integer.valueOf(walletId));
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TransferActivity.class);
        context.startActivity(intent);
    }

    public void getWalletDetailOK(WalletDetailBean data) {
        if (data == null || data.getList() == null) {
            return;
        }
        if (data.getList().size() > 0) {
            walletCoins = data.getList();
            tvChooseCoin.setText(walletCoins.get(0).getP_name());
            tvUsable.setText(App.INSTANCE.getString(R.string.wallet_transferActivity8) + CommonUtil.dealCoinNum(walletCoins.get(0).getUsable(), walletCoins.get(0).getP_name()) + walletCoins.get(0).getP_name());
        }
    }

    public void toTransferOK() {
        ToastUtils.show(App.INSTANCE.getString(R.string.wallet_transferActivity11));
        etAmount.getText().clear();
        ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_TRANSFER).navigation();
    }
}
